package pl.enigmatic.math;

public class RangeDouble {

	public final static RangeDouble UNIT = new RangeDouble(true, 0, 1, true);
	public final static RangeDouble EXT_UNIT = new RangeDouble(true, -1, 1, true);

	private final double left;
	private final boolean closedLeft;
	private final double right;
	private final boolean closedRight;

	public RangeDouble(final boolean closedLeft, final double left, final double right, final boolean closedRight) {
		this.left = left;
		this.closedLeft = closedLeft;
		this.right = right;
		this.closedRight = closedRight;
	}

	public double getLeft() {
		return left;
	}

	public boolean isClosedLeft() {
		return closedLeft;
	}

	public double getRight() {
		return right;
	}

	public boolean isClosedRight() {
		return closedRight;
	}

	public boolean isInside(final double x) {
		return (left < x || closedLeft && left == x) && (x < right || closedRight && right == x);
	}

	public boolean isOutside(final double x) {
		return !isInside(x);
	}

	public boolean nonEmpty() {
		return left < right || left == right && closedLeft && closedRight;
	}

	public boolean isEmpty() {
		return !nonEmpty();
	}

	public double getLength() {
		return right - left;
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + " " + (closedLeft ? "[" : "(") + left + ", " + right + (closedRight ? "]" : ")");
	}
}
